/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.ui.dialog;

import com.googlecode.jaden.common.other.JadenException;
import com.googlecode.jaden.common.other.Utils;
import static com.googlecode.jaden.ui.Constants.LABEL_CANCEL_TEXT;
import static com.googlecode.jaden.ui.Constants.LABEL_OK_TEXT;
import com.googlecode.jaden.ui.UiUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

public abstract class BaseDialog extends JDialog {

    private static final String SUFFIX_X = "__x";
    private static final String SUFFIX_Y = "__y";

    private Preferences preferences;
    protected JButton okButton;
    protected JButton cancelButton;
    private JPanel east;
    private boolean mainSet;
    private boolean cancelRemoved;

    protected BaseDialog(Window owner, String title) {
        super(owner, Utils.translate(title), ModalityType.APPLICATION_MODAL);
    }

    protected void setMainPanel(JPanel mainPanel, Preferences preferences) {

        this.preferences = preferences;

        addWindowListener(new WindowAdapter() {
            public void windowClosed(WindowEvent e) {
                setDialogLocation();
            }
        });

        okButton = UiUtils.createButton(LABEL_OK_TEXT);
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    doOk();
                } catch (JadenException ex) {
                    Utils.handleException(ex);
                    JOptionPane.showMessageDialog(BaseDialog.this, ex.getMessage());
                }
            }
        });
        getRootPane().setDefaultButton(okButton);
        if (!cancelRemoved) {
            cancelButton = UiUtils.createButton(LABEL_CANCEL_TEXT);
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    doCancel();
                }
            });
        }

        setLayout(new BorderLayout());

        east = new JPanel(new GridLayout(0, 1, 5, 5));
        east.add(okButton);
        if (!cancelRemoved) {
            east.add(cancelButton);
        }

        mainPanel.setBorder(BorderFactory.createEtchedBorder());
        add(UiUtils.horizontalList(mainPanel), BorderLayout.CENTER);
        add(UiUtils.horizontalList(east), BorderLayout.EAST);

        pack();
        setResizable(false);

        int x = preferences.getInt(getClass().getSimpleName() + SUFFIX_X, -1);
        int y = preferences.getInt(getClass().getSimpleName() + SUFFIX_Y, -1);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        if (x < 0 || y < 0 || x > screenSize.width ||
                y > screenSize.height) {
            x = (screenSize.width - getWidth()) / 2;
            y = (screenSize.height - getHeight()) / 2;
        }

        setLocation(x, y);

        mainSet = true;
    }

    protected void addButton(JButton button) {
        east.add(button);
        pack();
    }

    protected void setSouth(JComponent south) {
        add(UiUtils.horizontalList(south), BorderLayout.SOUTH);
        pack();
    }

    protected abstract void doOk() throws JadenException;

    private void doCancel() {
        dispose();
    }

    private void setDialogLocation() {
        String simpleName = getClass().getSimpleName();
        preferences.putInt(simpleName + SUFFIX_X, getX());
        preferences.putInt(simpleName + SUFFIX_Y, getY());
        try {
            preferences.flush();
        } catch (BackingStoreException e) {
            Utils.handleException(e);
        }
    }

    protected Preferences getPreferences() {
        return preferences;
    }

    public void setVisible(boolean b) {
        if (!mainSet) {
            throw new IllegalStateException("Main is not set yet");
        }
        super.setVisible(b);
    }


    protected void removeCancel() {
        cancelRemoved = true;
    }

}
